//
//  HLPickerManager.m
//  YueYue
//
//  Created by hulu001 on 2023/5/17.
//

#import "HLPickerManager.h"

@interface HLPickerManager ()

@property (nonatomic, assign) HXPhotoManagerSelectedType type;
@property (nonatomic, assign) NSInteger maxNum;
@property (nonatomic, strong) HXPhotoManager *manager;
@property (nonatomic, assign) BOOL isAvatar;
@end

@implementation HLPickerManager


- (instancetype)initWithType:(HXPhotoManagerSelectedType)type avatar:(BOOL)isAvatar
{
    self = [super init];
    if (self){
        self.type = type;
        self.manager = [[HXPhotoManager alloc] initWithType:self.type];
        self.maxNum = 1;
        self.isAvatar = isAvatar;
        [self configHXPicker];
    }
    return self;
}

- (void)updateMaxNum:(NSInteger)maxNum photos:(nullable NSArray<HXPhotoModel *> *)photos
{
    [_manager clearSelectedList];
    if (photos != nil){
        [_manager addLocalModels:photos];
    }
    self.maxNum = maxNum;
    [self configHXPicker];
}

- (void)configHXPicker
{
    _manager.configuration.deleteTemporaryPhoto = NO;
    _manager.configuration.creationDateSort = YES;//
    _manager.configuration.openCamera = YES; // 相册中有拍照
    _manager.configuration.maxNum = self.maxNum;       // 最大选择数：照片+视频
    _manager.configuration.photoMaxNum = self.maxNum;  // 最大照片选择数
    _manager.configuration.reverseDate = YES;// 正序显示相册
    if (self.isAvatar){
        _manager.configuration.photoEditConfigur.aspectRatio = HXPhotoEditAspectRatioType_1x1;// 配置裁剪比例
        _manager.configuration.photoEditConfigur.onlyCliping = YES;
        _manager.configuration.singleSelected = YES; // 单选模式
        _manager.configuration.singleJumpEdit = YES; // 单选模式下选择图片时是否直接跳转到编辑界面
        _manager.configuration.cameraPhotoJumpEdit = YES;// 拍照玩，直接进入裁剪
    }
    // UI
    _manager.configuration.themeColor = HLMainColor;// 主题色
    _manager.configuration.navBarTranslucent = NO;
}

- (void)showWithController:(UIViewController *)controller block:(HLPickerBlock)block
{
    [controller hx_presentSelectPhotoControllerWithManager:self.manager didDone:^(NSArray<HXPhotoModel *> * _Nullable allList, NSArray<HXPhotoModel *> * _Nullable photoList, NSArray<HXPhotoModel *> * _Nullable videoList, BOOL isOriginal, UIViewController * _Nullable viewController, HXPhotoManager * _Nullable manager) {
        if (block){
            block(allList);
        }
    } cancel:^(UIViewController * _Nullable viewController, HXPhotoManager * _Nullable manager) {
        
    }];
}

+ (void)previewWithManager:(HXPhotoManager *)manager
                     index:(NSInteger)index
                controller:(UIViewController *)viewController
{
    HXPhotoPreviewViewController *vc = [[HXPhotoPreviewViewController alloc] init];
    vc.outside = YES;
    vc.manager = manager;
    vc.exteriorPreviewStyle = HXPhotoViewPreViewShowStyleDark;
    vc.delegate = (id)viewController;
    if (manager.afterSelectedArray) {
        vc.modelArray = [NSMutableArray arrayWithArray:manager.afterSelectedArray];
    }
    if (index >= vc.modelArray.count) {
        vc.currentModelIndex = vc.modelArray.count - 1;
    }else if (index < 0) {
        vc.currentModelIndex = 0;
    }else {
        vc.currentModelIndex = index;
    }
    vc.showBottomPageControl = YES;
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.modalPresentationCapturesStatusBarAppearance = YES;
    [viewController presentViewController:vc animated:NO completion:nil];
}


@end
